package sun.sequence.data;

import java.io.Serializable;
import java.util.Objects;
import java.util.Set;

public class Result<T> implements Serializable {

    private Integer code;

    private String message;

    private T data;

    public static final Integer SUCCESS_CODE = 0;

    private static final String SUCCESS_MESSAGE = "成功";

    public static final Integer ERROR_CODE = 500;

    private static final String ERROR_MESSAGE = "系统错误";

    public Result(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Result() {
    }

    public boolean isSuccess() {
        return Objects.equals(SUCCESS_CODE, code);
    }


    public boolean isSuccess(Set<Integer> successCodeSet) {
        return successCodeSet.contains(code);
    }

    public boolean isFailed() {
        return !Objects.equals(SUCCESS_CODE, code);
    }


    public boolean isFailed(Set<Integer> failedCodeSet) {
        return failedCodeSet.contains(code);
    }

    public T getData(T defaultVal) {
        if (data == null) {
            return defaultVal;
        }
        return data;
    }


    public static <T> Result<T> success() {
        return new Result<>(SUCCESS_CODE, SUCCESS_MESSAGE, null);
    }

    public static <T> Result<T> success(T data) {
        return new Result<T>(SUCCESS_CODE, SUCCESS_MESSAGE, data);
    }

    public static <T> Result<T> error() {
        return new Result<>(ERROR_CODE, ERROR_MESSAGE, null);
    }


    public static <T> Result<T> error(String message) {
        return new Result<>(ERROR_CODE, message, null);
    }


    public static <T> Result<T> error(Integer code, String message) {
        return new Result<>(code, message, null);
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}